// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Load/SPCDParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/ISPCDService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (SPCD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/SPCD/SPCD.md>
*/

// 以下需要修改
enum SPCD1Index : std::uint8_t
{
    SPCD1Index_SID = 1,
    SPCD1Index_G1  = 2,
    SPCD1Index_C1  = 3,  // Component numbers. See Remark  10. (0 < Integer < 6; any unique combination of
                         // Integers 1 through 6 with no embedded blanks for grid points; Integer 0, 1 or blank for
                         // scalar points)
    SPCD1Index_D1 = 4,   // Value of enforced motion for components Ci at grid Gi. (Real)
    SPCD1Index_G2 = 5,
    SPCD1Index_C2 = 6,
    SPCD1Index_D2 = 7,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseSPCD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::SPCDData tempData1;
    SG::DataStructure::FEM::SPCDData tempData2;

    // TODO(bug 001) m_dominId的正确填写
    tempData1.m_domainId = 1;
    tempData2.m_domainId = 1;

    // 初始化当前字段的索引值
    int curIDIndex = 0;

    // 标记是否有第二个坐标系定义
    bool isHaveSecond = false;

    // 遍历分割后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前字段是否为空白，如果为空则跳过
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDIndex++;  // 增加索引以便继续遍历
            continue;
        }

        switch (curIDIndex)
        {
        case SPCD1Index_SID:
            tempData1.m_sId = convertToI64 (curStrKey);
            break;
        case SPCD1Index_G1:
            tempData1.m_g = convertToI64 (curStrKey);
            break;
        case SPCD1Index_C1:
            tempData1.m_c = convertToI64 (curStrKey);
            break;
        case SPCD1Index_D1:

            tempData1.m_d = convertToReal (curStrKey);

            break;
        case SPCD1Index_G2:
            isHaveSecond  = true;
            tempData2.m_g = convertToI64 (curStrKey);
            break;
        case SPCD1Index_C2:
            tempData2.m_c = convertToI64 (curStrKey);
            break;
        case SPCD1Index_D2:
            tempData2.m_d = convertToReal (curStrKey);
            break;
        default:
            break;
        }
        // 增加当前字段的索引以便处理下一个字段
        curIDIndex++;
    }

    // 如果存在第二个坐标系定义，则将其添加到数据库
    if (isHaveSecond)
    {
        if (dbServiceFactory->get<ISPCDService> ()->Add (tempData2) == Common::Status::STATUS_Failure)
        {
            throw std::runtime_error ("Failed to add SPCD data to database.");
            return Common::Status::STATUS_Failure;
        }
    }

    // 将第一个坐标系的数据添加到数据库
    return dbServiceFactory->get<ISPCDService> ()->Add (tempData1);
}